package com.cskaoyan.controller;

import com.cskaoyan.bean.MarketPermission;
import com.cskaoyan.bean.MarketPermissions;
import com.cskaoyan.bean.MarketRole;
import com.cskaoyan.bean.bo.MarketPermissionBo;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.MarketPermissionsVo;
import com.cskaoyan.bean.vo.MarketRoleVo;
import com.cskaoyan.service.AdminAdminService;
import com.cskaoyan.service.AdminRoleService;
import com.fasterxml.jackson.databind.ser.Serializers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 查询管理员类别的业务逻辑
 *
 * @author gatsby
 * @since 2022/10/11 23:01
 */
@RestController
@RequestMapping("admin/role")
public class AdminRoleController {

    @Autowired
    AdminRoleService adminRoleService;

    /**
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author gatsby
     * @since 2022/10/12 16:42
     */
    @RequestMapping("options")
    public BaseRespVo options() {
        CommonData<MarketRoleVo> commonData = adminRoleService.queryAdminRole();
        return BaseRespVo.ok(commonData, "成功");

    }

    /**
     * 显示管理员橘色信息
     *
     * @param basePageInfo
     * @param name
     * @return com.cskaoyan.bean.common.BaseRespVo<com.cskaoyan.bean.common.CommonData>
     * @author gatsby
     * @since 2022/10/12 17:22
     */
    @RequestMapping("list")
    public BaseRespVo<CommonData> list(BasePageInfo basePageInfo, String name) {
        BaseRespVo<CommonData> baseRespVo = adminRoleService.queryAdminLimit(basePageInfo, name);
        return baseRespVo;
    }

    /**
     * 创建管理员角色信息的业务逻辑
     *
     * @param
     * @param
     * @return com.cskaoyan.bean.common.BaseRespVo<com.cskaoyan.bean.MarketRole>
     * @author gatsby
     * @since 2022/10/12 17:22
     */
    @RequestMapping("create")
    public BaseRespVo<MarketRole> create( @RequestBody MarketRole marketRole1) {
        MarketRole marketRole = adminRoleService.createRole(marketRole1);
        if (marketRole.getId() == -1) {
            return BaseRespVo.error("角色已经存在");
        }
        return BaseRespVo.ok(marketRole, "成功");

    }

    @RequestMapping("update")
    public BaseRespVo<MarketRole> update(@RequestBody MarketRole marketRole){
        int code=adminRoleService.updateRole(marketRole);
        if(code==0){
            return BaseRespVo.ok("成功");
        }else{
            return new BaseRespVo(null,"系统内部错误",502);
        }
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketRole marketRole){
        adminRoleService.deleteRole(marketRole);
        return BaseRespVo.ok("成功");
    }

    @GetMapping("permissions")
    public BaseRespVo permissions(){
        MarketPermissionsVo marketPermissionsVo =adminRoleService.queryAllPermissions();
        return BaseRespVo.ok(marketPermissionsVo,"成功");

    }

    @PostMapping("permissions")
    public BaseRespVo permissionsWithRolesId(@RequestBody MarketPermissionBo marketPermissionBo){
        List<String> permissions=marketPermissionBo.getPermissions();
        Integer roleId=marketPermissionBo.getRoleId();
        adminRoleService.authPermissions(permissions,roleId);
        return BaseRespVo.ok("成功");

    }




}
